//
//  PhotoBrowerAnmator.swift
//  glzbowei
//
//  Created by 高伦展 on 2018/3/6.
//  Copyright © 2018年 glz. All rights reserved.
//

import UIKit

// MARK: - 展示动画协议
protocol PhotoBrowserPresentDelegate:NSObjectProtocol {
    
    //指定 indexPath 对应的 imageView，用来做动画效果
    func imageViewForPresent(indexPath:IndexPath) -> UIImageView
    //动画转场的起始位置
    func photoBrowserPresentFromRect(indexPath:IndexPath) ->CGRect
    //动画转场的目标位置
    func photoBrowserPresentToRect(indexPath:IndexPath) ->CGRect
}

// MARK: - 解除动画协议
protocol PhotoBrowserDismissDelegate:NSObjectProtocol {
    //解除转场的图像视图（包含起始位置）
    func imageViewForDismiss() -> UIImageView
    //解除转场的图像索引
    func indexPathForDismiss() ->IndexPath
    
}

//提供动画转场的’代理‘
class PhotoBrowserAnimator: NSObject,UIViewControllerTransitioningDelegate {

    weak var presentDelegate: PhotoBrowserPresentDelegate?
    
    weak var dismissDelegate:PhotoBrowserDismissDelegate?
    
    var indexPath:IndexPath?
    
    //是否 modal 展现的标签
    private var isPresented = false
    
    func setDelegateParams(presentDelegate:PhotoBrowserPresentDelegate,
                           indexPath:IndexPath,
                           dismissDelegate:PhotoBrowserDismissDelegate){
        self.presentDelegate = presentDelegate
        self.indexPath = indexPath
        self.dismissDelegate = dismissDelegate
    }
    
    //返回提供 modal 展现的‘动画的对象’
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
       
        isPresented = true
        
        return self
    }
    
    //返回提供 dismiss 的‘动画对象’
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = false
        
        return self
    }
}

//实现具体的动画方法
extension PhotoBrowserAnimator:UIViewControllerAnimatedTransitioning{
    
    //动画时长
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    //实现具体的动画效果 - 一旦实现了此方法，所有的动画代码都由程序员来编写
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
         let fromVC =  transitionContext.viewController(forKey:UITransitionContextViewControllerKey.from)
        
         let toVC =  transitionContext.viewController(forKey:UITransitionContextViewControllerKey.to)
        
        let fromView =  transitionContext.view(forKey: UITransitionContextViewKey.from)
        
        let toView =  transitionContext.view(forKey: UITransitionContextViewKey.to)
    
        isPresented ? presentAnimation(using: transitionContext) : dismissAnimation(using: transitionContext)
    }
    
    //解除转场动画
    private func dismissAnimation(using transitionContext: UIViewControllerContextTransitioning){
        
        guard let presentDelegate = presentDelegate,
            let dismissDelegate = dismissDelegate else{
            return
        }
        
        let fromView =  transitionContext.view(forKey: UITransitionContextViewKey.from)
        
        fromView?.removeFromSuperview()
        
        let iv = dismissDelegate.imageViewForDismiss()
        transitionContext.containerView.addSubview(iv)
        
        let indexPath = dismissDelegate.indexPathForDismiss()
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            iv.frame = presentDelegate.photoBrowserPresentFromRect(indexPath: indexPath)
            //fromView?.alpha = 0
        }) { (_) in
            
            fromView?.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
    }
    
    //展现试图
    private func presentAnimation(using transitionContext: UIViewControllerContextTransitioning){
        
        guard let presentDelegate = presentDelegate,let indexPath = indexPath else{
            return
        }
        
        let toView =  transitionContext.view(forKey: UITransitionContextViewKey.to)
        
        transitionContext.containerView.addSubview(toView!)
        
        let iv = presentDelegate.imageViewForPresent(indexPath: indexPath)
        
        iv.frame = presentDelegate.photoBrowserPresentFromRect(indexPath: indexPath)
        
        transitionContext.containerView.addSubview(iv)
        
        toView?.alpha = 0
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            iv.frame = presentDelegate.photoBrowserPresentToRect(indexPath: indexPath)
            toView?.alpha = 1
        }) { (_) in
            iv.removeFromSuperview()
            
             transitionContext.completeTransition(true)
        }
    }
}
